/**
 * @Author：ming
 * @Date：2023/10/4 7:36 PM
 * @Filename：Lc1094
 */

import java.util.*;

/**
 * @author ming
 */
public class Lc1094 {
    public boolean carPooling(int[][] trips, int capacity) {
        ArrayList<int[]> list = new ArrayList<>();

        Arrays.sort(trips, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1]-o2[1];
            }
        });

        for (int[] temp : trips) {
            //先下车
            Iterator<int[]> iterator = list.iterator();
            while (iterator.hasNext()){
                int[] a = iterator.next();
                if(a[2]<=temp[1]){
                    iterator.remove();
                    capacity +=a[0];
                }
            }
            if(capacity>=temp[0]){
                list.add(temp);
                capacity -= temp[0];
            }else {
                return false;
            }
        }

        return true;


    }
}
